package Servlet;

import DAO.DAOFactory;
import DAO.PackageDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/deletePackage")
public class deletePackageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int PackageID = Integer.parseInt(req.getParameter("PackageID"));
        PackageDAO packageDAO = (PackageDAO) DAOFactory.getPackageDAOInstance();
        PrintWriter out = resp.getWriter();
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        try {
            if(packageDAO.deletePackage(PackageID)){
                out.print("<script>alert('delete success!'); window.location='PackageManager' </script>");
            }else{
                out.print("<script>alert('delete fail!'); window.location='PackageManager' </script>");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
